﻿// HSS.Logging.LogRecord.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       LogRecord.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Logging
{
	#region Using Directives
	using System;
	using System.ComponentModel;
	using System.Data;
	using System.Runtime.Serialization;
	using System.Xml.Serialization;
	using HSS.Data;
	#endregion

	#region LogRecord
	/// <summary>
	/// The Log Record Object Model
	/// </summary>
	[Serializable]
	public class LogRecord : INotifyPropertyChanged, IDataContainer, IDataEntity
	{
		#region Constructors
		/// <summary>
		/// Initializes a new instance of the LogRecord class.
		/// </summary>
		public LogRecord()
		{
		}
		/// <summary>
		/// Initializes a new instance of the LogRecord class.
		/// </summary>
		public LogRecord(Int32 eventId, Int32 eventTypeID, String eventType, String message, String applicationName, String referenceNumber, Int32 categoryID, String category, DateTime createdDate, String userName, String viewAttachment, String viewAttachmentLabel)
		{
			this.EventId = eventId;
			this.EventTypeID = eventTypeID;
			this.EventType = eventType;
			this.Message = message;
			this.ApplicationName = applicationName;
			this.ReferenceNumber = referenceNumber;
			this.CategoryID = categoryID;
			this.Category = category;
			this.CreatedDate = createdDate;
			this.UserName = userName;
			this.ViewAttachment = viewAttachment;
			this.ViewAttachmentLabel = viewAttachmentLabel;
		}
		/// <summary>
		/// Initializes a new instance of the LogRecord class.
		/// </summary>
		/// <param name="dr">A <see cref="System.Data.IDataRecord"/> containing the data for this Entity</param>
		public LogRecord(IDataRecord dr)
		{
			this.Load(dr);
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the EventId value.
		/// </summary>
		public virtual Int32 EventId
		{
			get { return _eventId; }
			set
			{
				_eventId = value;
				this.OnPropertyChanged("EventId");
			}
		} Int32 _eventId;
		/// <summary>
		/// Gets or sets the EventTypeID value.
		/// </summary>
		public virtual Int32 EventTypeID
		{
			get { return _eventTypeID; }
			set
			{
				_eventTypeID = value;
				this.OnPropertyChanged("EventTypeID");
			}
		} Int32 _eventTypeID;
		/// <summary>
		/// Gets or sets the EventType value.
		/// </summary>
		public virtual String EventType
		{
			get { return _eventType; }
			set
			{
				_eventType = value;
				this.OnPropertyChanged("EventType");
				this.OnPropertyChanged("EventTypeImage");
			}
		} String _eventType;
		/// <summary>
		/// Gets or sets the Message value.
		/// </summary>
		public virtual String Message
		{
			get { return _message; }
			set
			{
				_message = value;
				this.OnPropertyChanged("Message");
			}
		} String _message;
		/// <summary>
		/// Gets or sets the name of the application this record is associated with.
		/// </summary>
		public virtual String ApplicationName
		{
			get { return _applicationName; }
			set
			{
				_applicationName = value;
				this.OnPropertyChanged("ApplicationName");
			}
		} String _applicationName;
		/// <summary>
		/// Gets or sets the ReferenceNumber value.
		/// </summary>
		public virtual String ReferenceNumber
		{
			get { return _referenceNumber; }
			set
			{
				_referenceNumber = value;
				this.OnPropertyChanged("ReferenceNumber");
			}
		} String _referenceNumber;
		/// <summary>
		/// Gets or sets the CategoryID value.
		/// </summary>
		public virtual Int32 CategoryID
		{
			get { return _categoryID; }
			set
			{
				_categoryID = value;
				this.OnPropertyChanged("CategoryID");
			}
		} Int32 _categoryID;
		/// <summary>
		/// Gets or sets the Category value.
		/// </summary>
		public virtual String Category
		{
			get { return _category; }
			set
			{
				_category = value;
				this.OnPropertyChanged("Category");
			}
		} String _category;
		/// <summary>
		/// Gets or sets the CreatedDate value.
		/// </summary>
		public virtual DateTime CreatedDate
		{
			get { return _createdDate; }
			set
			{
				_createdDate = value;
				this.OnPropertyChanged("CreatedDate");
			}
		} DateTime _createdDate;
		/// <summary>
		/// Gets or sets the UserName value.
		/// </summary>
		public virtual String UserName
		{
			get { return _userName; }
			set
			{
				_userName = value;
				this.OnPropertyChanged("UserName");
			}
		} String _userName;
		/// <summary>
		/// Gets or sets the HasAttachment value.
		/// </summary>
		public virtual Boolean HasAttachment
		{
			get { return this._hasAttachment; }
			set
			{
				if (this._hasAttachment != value)
				{
					this._hasAttachment = value;
					this.OnPropertyChanged("HasAttachment");
				}
			}
		} bool _hasAttachment;
		/// <summary>
		/// Gets or sets the ViewAttachment value.
		/// </summary>
		public virtual String ViewAttachment
		{
			get { return _viewAttachment; }
			set
			{
				_viewAttachment = value;
				this.OnPropertyChanged("ViewAttachment");
				var attach = this._viewAttachment.Replace("AttachmentViewer.aspx?AttachmentId=", string.Empty);
				if (attach.Length > 0)
					this.HasAttachment = true;
				else
					this.HasAttachment = false;
			}
		} String _viewAttachment;
		/// <summary>
		/// Gets or sets the ViewAttachmentLabel value.
		/// </summary>
		public virtual String ViewAttachmentLabel
		{
			get { return _viewAttachmentLabel; }
			set
			{
				_viewAttachmentLabel = value;
				this.OnPropertyChanged("ViewAttachmentLabel");
			}
		} String _viewAttachmentLabel;
		/// <summary>
		/// Gets the Image Source Path for the EventType.
		/// </summary>
		public virtual String EventTypeImage
		{
			get
			{
				switch (this.EventType)
				{
					case "Error":
						return "/HSS.Diagnostics.Management;component/Resources/eventLogError.ico";
					case "FailureAudit":
						return "/HSS.Diagnostics.Management;component/Resources/eventLogFailureAudit.ico";
					case "Information":
						return "/HSS.Diagnostics.Management;component/Resources/eventLogInformation.ico";
					case "SuccessAudit":
						return "/HSS.Diagnostics.Management;component/Resources/eventLogSuccessAudit.ico";
					case "Warning":
						return "/HSS.Diagnostics.Management;component/Resources/eventLogWarning.ico";
					default:
						return "/HSS.Diagnostics.Management;component/Resources/eventLogInformation.ico";
				}
			}
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Returns the Primary Key of the object.
		/// </summary>
		/// <returns>String</returns>
		public override String ToString()
		{
			return "[LogRecord] " + this.EventId.ToString();
		}
		#endregion

		#region IDataEntity Members
		/// <summary>
		/// Load this instance from a <see cref="System.Data.IDataRecord"/>.
		/// </summary>
		/// <param name="dr">A <see cref="System.Data.IDataRecord"/> containing the data to load this Entity with.</param>
		public void Load(IDataRecord dr)
		{
			this._eventId = dr.GetInt32(dr.GetOrdinal("EventId"));
			this._eventTypeID = dr.GetInt32(dr.GetOrdinal("EventTypeID"));
			this._eventType = dr.GetString(dr.GetOrdinal("EventType"));
			if (!dr.IsDBNull(dr.GetOrdinal("Message")))
				this._message = dr.GetString(dr.GetOrdinal("Message"));
			if (!dr.IsDBNull(dr.GetOrdinal("ApplicationName")))
				this._applicationName = dr.GetString(dr.GetOrdinal("ApplicationName"));
			if (!dr.IsDBNull(dr.GetOrdinal("ReferenceNumber")))
				this._referenceNumber = dr.GetString(dr.GetOrdinal("ReferenceNumber"));
			this._categoryID = dr.GetInt32(dr.GetOrdinal("CategoryID"));
			if (!dr.IsDBNull(dr.GetOrdinal("Category")))
				this._category = dr.GetString(dr.GetOrdinal("Category"));
			this._createdDate = dr.GetDateTime(dr.GetOrdinal("CreatedDate"));
			this._userName = dr.GetString(dr.GetOrdinal("UserName"));
			if (!dr.IsDBNull(dr.GetOrdinal("ViewAttachment")))
			{
				this._viewAttachment = dr.GetString(dr.GetOrdinal("ViewAttachment"));
				var attach = this._viewAttachment.Replace("AttachmentViewer.aspx?AttachmentId=", string.Empty);
				if (attach.Length > 0)
					this._hasAttachment = true;
				else
					this._hasAttachment = false;
			}
			this._viewAttachmentLabel = dr.GetString(dr.GetOrdinal("ViewAttachmentLabel"));
		}

		#endregion

		#region INotifyPropertyChanged
		/// <summary>
		/// Occurs when a property has changed.
		/// </summary>
		public event PropertyChangedEventHandler PropertyChanged;
		/// <summary>
		/// Call this method when a Property's set accessor changes the value.
		/// </summary>
		/// <param name="propertyName">The name of the Property being effected.</param>
		protected void OnPropertyChanged(string propertyName)
		{
			var handler = this.PropertyChanged;
			if (null != handler)
				handler(this, new PropertyChangedEventArgs(propertyName));
		}
		#endregion

		#region IDataContainer
		/// <summary>
		/// Gets the Id of this record.
		/// </summary>
		[XmlIgnore]
		public string Id
		{
			get { return this.EventId.ToString(); }
			set { }
		}
		/// <summary>
		/// Gets the Name of this record (Id);
		/// </summary>
		[XmlIgnore]
		public string Name
		{
			get { return this.Id; }
			set { }
		}
		/// <summary>
		/// Gets this instance as an Object.
		/// </summary>
		[XmlIgnore]
		public object Data
		{
			get { return null; }
			set { }
		}
		#endregion
	}
	#endregion
}